<script type="text/html" template lay-done="layui.data.done(d);">
    <div class="layui-form coreshop-form layui-form-pane" lay-filter="LAY-app-WeChatShippingOrder-createForm" id="LAY-app-WeChatShippingOrder-createForm">
        <input type="hidden" name="transaction_id" value="{{d.params.data.order.transaction_id }}" />
        <input type="hidden" name="openid" value="{{d.params.data.order.openid }}" />
        <input type="hidden" name="merchant_id" value="{{d.params.data.order.merchant_id }}" />
        <input type="hidden" name="merchant_trade_no" value="{{d.params.data.order.merchant_trade_no }}" />

        <blockquote class="layui-elem-quote">
            商户单号：{{d.params.data.order.merchant_trade_no }}<br />
            交易单号：{{d.params.data.order.transaction_id }}<br />
            支付金额：¥ {{d.params.data.order.paid_amount / 100 }} 元<br />
            支付时间：{{ layui.util.toDateString(d.params.data.order.pay_time * 1000, 'yyyy-MM-dd HH:mm:ss')}}<br />
        </blockquote>


        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">商品信息</label>
            <div class="layui-input-block">
                <textarea name="item_desc" placeholder="商品信息会向用户展示，请保持和订单信息一致。输入多个商品时用“ ; ”隔开。" class="layui-textarea">{{d.params.data.orderTitle}}</textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">发货方式</label>
            <div class="layui-input-block">
                {{# layui.each(d.params.data.logisticsType, function(index, item){ }}
                <input type="radio" name="logistics_type" value="{{item.value}}" title="{{item.title}}" {{item.value==d.params.data.logisticsTypeValue ? 'checked':''}} lay-filter="logistics-type-filter">
                {{# }); }}
            </div>
        </div>


        <div id="logisticsBox">
            <div class="layui-form-item">
                <label for="matchKey" class="layui-form-label ">
                    快递信息
                </label>
                <div class="layui-input-block">
                    <table class="layui-table">
                        <thead>
                            <tr>
                                <th>物流公司</th>
                                <th style="width: 250px;">快递单号</th>
                                <th style="width: 82px;">操作</th>
                            </tr>
                        </thead>
                        <tbody id="logisticsView">
                            <tr data-id="0">
                                <td>
                                    <select lay-search="" id="express_company" name="shipping_list.express_company[0]" class="layui-input">
                                        <option value="">请选择，可直接输入搜索</option>
                                        {{# layui.each(d.params.data.deliveryCompany, function(index, item){ }}
                                        <option value="{{item.deliveryId}}">{{item.deliveryName}}</option>
                                        {{# }); }}
                                    </select>
                                </td>
                                <td class="field_value">
                                    <input type="text" id="tracking_no" name="shipping_list.tracking_no[0]" required value="" placeholder="填写快递单号" autocomplete="off" class="layui-input">
                                </td>
                                <td>
                                    <a class="layui-btn layui-btn-xs addfield-class table-button">
                                        添加
                                    </a>
                                    <!--<a class="layui-btn layui-btn-danger layui-btn-xs del-class table-button">
                                        删除
                                    </a>-->
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">全部发货</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="is_all_delivered" lay-skin="switch" lay-filter="switchTest" checked="checked" title="是|否">
                </div>
            </div>
            {{# if(d.params.data.billDeliveryList){ }}
            <div class="layui-bg-gray" style="padding: 16px;">
                <div class="layui-row layui-col-space15">
                    <div class="layui-col-md12">
                        <div class="layui-card">
                            <div class="layui-card-header">订单发货信息</div>
                            <div class="layui-card-body">
                                {{# layui.each(d.params.data.billDeliveryList, function(index, item){ }}
                                发货快递：{{item.logiCode}}  快递编号：{{item.logiNo}}
                                {{# }); }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {{# } }}

        </div>

        <div id="cityDistributionBox" style="display:none;">
            <div class="layui-form-item">
                <label for="matchKey" class="layui-form-label">
                    快递信息
                </label>
                <div class="layui-input-inline layui-inline-4">
                    <input name="cityDistributionName" class="layui-input" placeholder="填写同城配送公司名称" />
                </div>
                <div class="layui-input-inline layui-inline-4">
                    <input name="cityDistributionNumber" class="layui-input" placeholder="填写快递单号" />
                </div>
                <div class="layui-form-mid">
                    如商家自配，则该项可选填
                </div>
            </div>
        </div>

        <div class="layui-form-item text-right core-hidden">
            <input type="button" class="layui-btn" lay-submit lay-filter="LAY-app-WeChatShippingOrder-createForm-submit" id="LAY-app-WeChatShippingOrder-createForm-submit" value="确认添加">
        </div>
    </div>
</script>


<script id="tr_tpl" type="text/html">
    <tr data-id="{{ d.id }}">
        <td>
            <select lay-search="" id="express_company" name="shipping_list.express_company[{{ d.id }}]" class="layui-input">
                <option value="">请选择，可直接输入搜索</option>
                {{# layui.each(d.deliveryCompany, function(index, item){ }}
                <option value="{{item.deliveryId}}">{{item.deliveryName}}</option>
                {{# }); }}
            </select>
        </td>
        <td class="field_value">
            <input type="text" id="tracking_no" name="shipping_list.tracking_no[{{ d.id }}]" required value="" placeholder="填写快递单号" autocomplete="off" class="layui-input">
        </td>
        <td>
            <a class="layui-btn layui-btn-xs addfield-class table-button">
                添加
            </a>
            <a class="layui-btn layui-btn-danger layui-btn-xs del-class table-button">
                删除
            </a>
        </td>
    </tr>
</script>



<script>
    var data;
    var debug = layui.setter.debug;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d.params.data); }
        data = d.params.data;
        layui.use(['admin', 'form', 'laydate', 'upload', 'coreHelper', 'cropperImg', 'laytpl'],
            function () {
                var $ = layui.$
                    , form = layui.form
                    , admin = layui.admin
                    , laydate = layui.laydate
                    , upload = layui.upload
                    , cropperImg = layui.cropperImg
                    , laytpl = layui.laytpl
                    , coreHelper = layui.coreHelper;

                hideBox(d.params.data.logisticsTypeValue);

                $(".layui-table").on('click', '.addfield-class', function (e) {
                    var getTpl = tr_tpl.innerHTML;
                    var lastId = $(this).parent().parent().parent().find('tr').last().attr('data-id');
                    console.log(lastId);
                    var tmpData = {};
                    tmpData.id = parseInt(lastId) + 1;
                    tmpData.deliveryCompany = d.params.data.deliveryCompany;
                    laytpl(getTpl).render(tmpData, function (html) {
                        $("#logisticsView").append(html);
                        form.render();
                    });
                });

                $(".layui-table").on('click', '.del-class', function (e) {
                    if ($(".del-class").length > 0) {
                        $(this).parent().parent().remove();
                        resetInputNameID();
                    } else {
                        layer.msg("至少保留1个物流信息录入框");
                    }
                })

                //重置排序
                function resetInputNameID() {
                    $.each($("#logisticsView tr"), function (i, tr) {
                        $(this).attr('data-id', i);
                        $(this).find("#express_company").attr("name", "shipping_list.express_company[" + i + "]");
                        $(this).find("#tracking_no").attr("name", "shipping_list.tracking_no[" + i + "]");
                    });
                }

                // radio 事件
                form.on('radio(logistics-type-filter)', function (data) {
                    var elem = data.elem; // 获得 radio 原始 DOM 对象
                    var value = elem.value; // 获得 radio 值

                    hideBox(value);
                });

                function hideBox(value) {
                    if (value == 1) {
                        $('#logisticsBox').show();
                        $('#cityDistributionBox').hide();
                    } else if (value == 2) {
                        $('#logisticsBox').hide();
                        $('#cityDistributionBox').show();
                    } else {
                        $('#logisticsBox').hide();
                        $('#cityDistributionBox').hide();
                    }
                }

                form.verify({

                    verifymatchKey: [/^.{0,100}$/, '匹配字符最大只允许输入100位字符'],
                    verifyimgTextUrl: [/^.{0,1000}$/, '图片回复图片地址最大只允许输入1000位字符'],
                    verifyimgTextLink: [/^.{0,1000}$/, '图片回复超链接最大只允许输入1000位字符'],
                    verifymeidaUrl: [/^.{0,1000}$/, '语音回复地址最大只允许输入1000位字符'],
                    verifymeidaLink: [/^.{0,1000}$/, '语音回复超链接最大只允许输入1000位字符'],
                    verifyremark: [/^.{0,1000}$/, '备注最大只允许输入1000位字符'],
                    verifycreateBy: [/^.{0,100}$/, '创建来源最大只允许输入100位字符'],
                    verifymodifyBy: [/^.{0,100}$/, '修改来源最大只允许输入100位字符'],
                });
                //重载form
                form.render(null, 'LAY-app-WeChatShippingOrder-createForm');
            })
    };
</script>
